package com.sinotrans.common.rest;

import java.util.Collection;

import com.baomidou.mybatisplus.core.metadata.IPage;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

@ApiModel(description = "分页查询实体")
public class PageEntity<T> extends RestEntity<T> {

	@ApiModelProperty(value = "当前页")
	protected Long pageNum = 1L;

	@ApiModelProperty(value = "每页数量")
	protected Long pageSize = 10L;

	@ApiModelProperty(value = "总数量")
	protected Long totalNum = 0L;

	public PageEntity() {
	}

	/**
	 * 包装{@link com.baomidou.mybatisplus.core.metadata.IPage}对象
	 *
	 * @param list
	 */
	public PageEntity(T t) {
		this.entity = t;
		if (t instanceof Collection<?>) {
			Collection<?> array = (Collection<?>) t;
			this.pageNum = 1L;
			this.pageSize = (long) array.size();
		}
		if (t instanceof IPage) {
			IPage<?> page = (IPage<?>) t;
			this.totalNum = page.getTotal();
			this.pageNum = page.getCurrent();
			this.pageSize = page.getSize();
		}
	}

	public static <T> PageEntity<T> of(T list) {
		return new PageEntity<T>(list);
	}

	public Long getPageNum() {
		return pageNum;
	}

	public void setPageNum(Long pageNum) {
		this.pageNum = pageNum;
	}

	public Long getPageSize() {
		return pageSize;
	}

	public void setPageSize(Long pageSize) {
		this.pageSize = pageSize;
	}

	public Long getTotalNum() {
		return totalNum;
	}

	public void setTotalNum(Long totalNum) {
		this.totalNum = totalNum;
	}

}
